package com.smart.sso.server.controller.admin;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smart.mvc.controller.BaseController;
import com.smart.mvc.model.Result;
import com.smart.mvc.validator.Validator;
import com.smart.mvc.validator.annotation.ValidateParam;
import com.smart.sso.server.model.Dept;
import com.smart.sso.server.service.DeptService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author Joe
 */
@Api(tags = "组织机构(含菜单)管理")
@Controller
@RequestMapping("/admin/dept")
public class DeptController extends BaseController {

    @Resource
    private DeptService deptService;

    @ApiOperation("初始页")
    @RequestMapping(method = RequestMethod.GET)
    public String execute(Model model) {
        model.addAttribute("deptList", getDeptList());
        return "/admin/dept";
    }

    @ApiOperation("权限树节点")
    @RequestMapping(value = "/nodes", method = RequestMethod.GET)
    public @ResponseBody List<Dept> nodes(
            @ApiParam(value = "名称") String name,
            @ApiParam(value = "是否启用 ") Boolean isEnable) {
        List<Dept> list = deptService.findByName(name, isEnable);
        Dept dept = new Dept();
        dept.setId(null);
        dept.setParentId(-1);
        dept.setName("根节点");
        list.add(0, dept);
        return list;
    }

    @ApiOperation("新增/修改提交")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public @ResponseBody Result save(
            @ApiParam(value = "id") Integer id,
            @ApiParam(value = "父id", required = true) Integer parentId,
            @ApiParam(value = "图标") String icon,
            @ApiParam(value = "名称", required = true) @ValidateParam({
                Validator.NOT_BLANK }) String name,
            @ApiParam(value = "权限URL", required = true) @ValidateParam({
                Validator.NOT_BLANK }) String url,
            @ApiParam(value = "排序", required = true) @ValidateParam({
                Validator.NOT_BLANK }) Integer sort,
            @ApiParam(value = "是否菜单", required = true) @ValidateParam({
                Validator.NOT_BLANK }) Boolean isMenu,
            @ApiParam(value = "是否启用", required = true) @ValidateParam({
                Validator.NOT_BLANK }) Boolean isEnable) {
        Dept dept;
        if (id == null) {
            dept = new Dept();
        } else {
            dept = deptService.get(id);
        }
        dept.setParentId(parentId);
        dept.setIcon(icon);
        dept.setName(name);
        dept.setUrl(url);
        dept.setSort(sort);
        dept.setIsMenu(isMenu);
        dept.setIsEnable(isEnable);
        deptService.save(dept);
        return Result.createSuccessResult().setMessage("保存成功");
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody Result delete(
            @ApiParam(value = "id", required = true) @ValidateParam({
                Validator.NOT_BLANK }) Integer id) {
        deptService.deleteById(id);
        return Result.createSuccessResult().setMessage("删除成功");
    }

    private List<Dept> getDeptList() {
        return deptService.findByName(null, null);
    }
}